/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.master;

import javax.swing.JPanel;

import com.melloware.jintellitype.JIntellitype;

import softstarter.gui.subareas.FireActionsController;
import softstarter.gui.subareas.commandline.CommandLine;
import softstarter.gui.subareas.controlbuttons.ControlButtons;
import softstarter.gui.subareas.controlbuttons.ControlButtonsController;
import softstarter.gui.subareas.settings.SettingsView;
import softstarter.gui.systemtray.SystemTrayFunctionality;
import softstarter.gui.systemtray.TrayMouseListener;

public class GUIStateController {

    private SoftStarterGUI masterWindow;
	private CommandLine commandLine;
	private ControlButtons controlButtons;
	private ControlButtonsController controlButtonsController;
	private FireActionsController fireActionsController;
	private DragAndDropHandler dragAndDropHandler;
	private SettingsView settingsView;
	private SystemTrayFunctionality systemTrayFunctionality;
	
	private boolean useWindowsHotkey = true;
	private boolean useSystemTray = true;
	
	private boolean isSettingsViewVisible;
	
	public GUIStateController(SoftStarterGUI masterWindow) {
	    this.masterWindow = masterWindow;
	    isSettingsViewVisible = false;
	    if(useWindowsHotkey)
	        registerWindowsHotkey();
		build();
	}
	
	public void build() {
	    fireActionsController = new FireActionsController(this);
		commandLine = new CommandLine(this, fireActionsController);
	    dragAndDropHandler = new DragAndDropHandler(masterWindow, this);
	    controlButtonsController = new ControlButtonsController(this);
	    controlButtons = new ControlButtons(controlButtonsController);
	    settingsView = new SettingsView(this);
	    TrayMouseListener trayMouseListener = new TrayMouseListener(this);
	    if(useSystemTray)
	        systemTrayFunctionality = new SystemTrayFunctionality(trayMouseListener, controlButtons.getListener());
	}
	
	public JPanel getCommandLineView() {
		return commandLine.getMasterContainer();
	}
	
	public CommandLine getCommandLine() {
	    return commandLine;
	}
	
	public JPanel getControllerButtonsView() {
		return controlButtons.getMasterContainer();
	}
	
	public void refreshGUI() {
	    masterWindow.pack();
	    masterWindow.repaint();
	}
	
	public void updateFileView() {
	    settingsView.buildListWithFilesAndRemoveButtons();
	}
	
	public void showSettings() {
	    if(isSettingsViewVisible) {
	        masterWindow.hideSettingsView();
	        isSettingsViewVisible = false;
	        commandLine.setFocusToCommandLine();
	    } else {
    	    masterWindow.showSettingsView(settingsView.getView());
    	    isSettingsViewVisible = true;
	    }
        refreshGUI();
	}
	
	private void registerWindowsHotkey() {
        
        JIntellitype.getInstance().registerHotKey(1,JIntellitype.MOD_WIN, (int)'S' );

        JIntellitype.getInstance().addHotKeyListener(new FiresoftHotkeyListener(this));
	
	}
	
	public void hideOrShow() {
	    if(masterWindow.isVisible()) {
	        masterWindow.setVisible(false);
	        commandLine.setFocusToCommandLine();
	    } else { 
	        masterWindow.setVisible(true);
	    }
	}
	
	public void clearCommandlineAndHideWindow() {
	    commandLine.clearTextArea();
	    masterWindow.setVisible(false);
	}
	
	public void closeProgram() {
		JIntellitype.getInstance().cleanUp();
		System.exit(0);
	}
		
}
